package com.dxl.system.controller;

import com.dxl.base.Result;
import com.dxl.system.model.Groups;
import com.dxl.system.service.GroupsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <b> 分组控制器
 * </b><br><br><i>Description</i> :
 * <br>Date: 2019-09-25    <br>Author : dxl
 */
@Api(value = "分组接口",tags = {"分组--接口"})
@Controller
@RequestMapping("/groups")
public class GroupsController {

    @Autowired(required = false)
    private GroupsService groupsService;


    /**
     * <b> 插入分组
     * </b><br><br><i>Description</i> : 不能为空：组名称-group_name ；不能重复：组名称-group_name  
     * @param groups Groups
     * @return com.dxl.base.Result Groups
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "插入分组Html",notes = "不能为空：组名称-group_name ；不能重复：组名称-group_name  ")
    @RequestMapping(value = "/addOne",method = {RequestMethod.POST })
    @ResponseBody
    public Result addOne(@Valid @RequestBody Groups groups) {
        Result result = groupsService.addOne(groups);
        return result;
    }

    /**
     * <b> 插入分组Html
     * </b><br><br><i>Description</i> : 不能为空：组名称-group_name ；不能重复：组名称-group_name
     * @return html
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "插入分组",notes = "不能为空：组名称-group_name ；不能重复：组名称-group_name  ")
    @GetMapping(value = "/addOneHtml")
    public String addOneHtml() {
        return "groups/groups_add";
    }

    /**
     * <b> 插入分组Html
     * </b><br><br><i>Description</i> : 不能为空：组名称-group_name ；不能重复：组名称-group_name
     * @return html
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "插入分组Html",notes = "不能为空：组名称-group_name ；不能重复：组名称-group_name  ")
    @GetMapping(value = "/addOneNodeHtml")
    public String addOneNodeHtml(@RequestParam(value="pId", required = true) Integer pId,Model model) {
        Groups groups = (Groups) groupsService.getById(pId).getData();
        model.addAttribute(groups);
        return "groups/groups_nodeadd";
    }

    /**
     * <b> 删除分组
     * </b><br><br><i>Description</i> : 
     * @param groupsIds List: Integer
     * @return com.dxl.base.Result int
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "删除分组",notes = "无注意点！")
    @RequestMapping(value = "/delByIds",method = {RequestMethod.POST })
    @ResponseBody
    public Result delByIds(@RequestParam(value="groupsIds", required = true) List<Integer> groupsIds){
        Result result = groupsService.delByIds(groupsIds);
        return result;
    }

    /**
     * <b> 删除分组及所有子分组和文件
     * </b><br><br><i>Description</i> :
     * @param groupsIds List: Integer
     * @return com.dxl.base.Result int
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "删除分组及所有子分组和文件",notes = "无注意点！")
    @RequestMapping(value = "/delJoinByIds",method = {RequestMethod.POST })
    @ResponseBody
    public Result delJoinByIds(@RequestParam(value="groupsIds", required = true) List<Integer> groupsIds){
        Result result = groupsService.delJoinByIds(groupsIds);
        return result;
    }

    /**
     * <b> 编辑分组
     * </b><br><br><i>Description</i> : 不能重复：组名称-group_name 
     * @param groups Groups
     * @return com.dxl.base.Result Groups
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "编辑分组",notes = "不能重复：组名称-group_name ")
    @RequestMapping(value = "/editOne",method = {RequestMethod.POST })
    @ResponseBody
    public Result editOne(@Valid @RequestBody Groups groups) {
        Result result = groupsService.editOne(groups);
        return result;
    }

    /**
     * <b> 编辑分组Html
     * </b><br><br><i>Description</i> : 不能重复：组名称-group_name
     * * @param [id, model]
     * @return String
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "编辑分组Html",notes = "不能重复：组名称-group_name ")
    @RequestMapping(value = "/editOneHtml",method = RequestMethod.GET)
    public String editOneHtml(@RequestParam(value="id", required = true) Integer id,Model model) {
        Groups groups = (Groups) groupsService.getById(id).getData();
        model.addAttribute(groups);
        return "groups/groups_edit";
    }

    /**
     * <b> ID查询分组
     * </b><br><br><i>Description</i> : 
     * @param groupsId Integer
     * @return com.dxl.base.Result: Groups
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "ID查询分组",notes = "无注意点！")
    @RequestMapping(value = "/getById",method = {RequestMethod.POST })
    @ResponseBody
    public Result getById(@RequestParam(value="groupsId", required = true) Integer groupsId) {
        Result result = groupsService.getById(groupsId);
        return result;
    }

    /**
     * <b> 条件查询分组
     * </b><br><br><i>Description</i> : 
     * @param groups Groups
     * @return com.dxl.base.Result List: Groups
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "条件查询分组",notes = "无注意点！")
    @RequestMapping(value = "/getList",method = {RequestMethod.POST })
    @ResponseBody
    public Result getList(@Valid @RequestBody Groups groups) {
        Result result = groupsService.getList(new Groups());
        return result;
    }

    /**
     * <b> 分页条件查询分组
     * </b><br><br><i>Description</i> : 
     * @param groups Groups, currentPage -Integer
     * @return com.dxl.base.Result List: Groups
     * <br><br>Date: 2019-09-25     <br>Author : dxl
     */
    @ApiOperation(value = "分页条件查询分组",notes = "无注意点！")
    @RequestMapping(value = "/getListByPage",method = {RequestMethod.POST })
    @ResponseBody
    public Result getListByPage(@Valid @RequestBody Groups groups, @RequestParam(value="currentPage", required = false, defaultValue = "1") Integer currentPage, @RequestParam(value="pageSize", required = false, defaultValue = "20") Integer pageSize) {
        Result result = groupsService.getListByPage(groups, currentPage, pageSize);
        return result;
    }

    /**
     * <b>  模糊查询分组
     * </b><br><br><i>Description</i> : 模糊查询；模糊字段(模式：单字段)：全匹配：group_name-组名称 
     * @param groups Groups
     * @return com.dxl.base.Result List: Groups
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "模糊查询分组",notes = "模糊查询；模糊字段(模式：单字段)：全匹配：group_name-组名称 ")
    @RequestMapping(value = "/getLikeGroups",method = {RequestMethod.POST })
    @ResponseBody
    public Result getLikeGroups(@Valid @RequestBody Groups groups){
        Result result = groupsService.getLikeGroups(groups);
        return result;
    }

    /**
     * <b> 分页模糊查询分组
     * </b><br><br><i>Description</i> : 模糊查询；模糊字段(模式：单字段)：全匹配：group_name-组名称 
     * @param groups Groups, currentPage -Integer, pageSize -Integer
     * @return com.dxl.base.Result List: Groups
     * <br><br>Date: 2019-09-25     <br>Author : dxl
     */
    @ApiOperation(value = "分页模糊查询分组",notes = "模糊查询；模糊字段(模式：单字段)：全匹配：group_name-组名称 ")
    @RequestMapping(value = "/getLikeGroupsByPage",method = {RequestMethod.POST })
    @ResponseBody
    public Result getLikeGroupsByPage(@Valid @RequestBody Groups groups, @RequestParam(value="currentPage", required = false, defaultValue = "1") Integer currentPage, @RequestParam(value="pageSize", required = false, defaultValue = "20") Integer pageSize){
        Result result = groupsService.getLikeGroupsByPage(groups, currentPage, pageSize);
        return result;
    }

    /**
     * <b> 分组能否新增
     * </b><br><br><i>Description</i> : 无权限限制；不能为空：组名称-group_name ；不能重复：组名称-group_name  
     * @param groups Groups
     * @return com.dxl.base.Result boolean
     * <br><br>Date: 2019-09-25     <br>Author : dxl
     */
    @ResponseBody
    @ApiOperation(value = "分组能否新增",notes = "无权限限制；不能为空：组名称-group_name ；不能重复：组名称-group_name  ")
    @RequestMapping(value = "/isAddable",method = {RequestMethod.POST })
    public Result isAddable(@Valid @RequestBody Groups groups) {
        Result result = groupsService.isGroupsAddable(groups);
        return result;
    }

    /**
     * <b> 分组能否编辑
     * </b><br><br><i>Description</i> : 无权限限制；不能重复：组名称-group_name 
     * @param groups Groups
     * @return com.dxl.base.Result boolean
     * <br><br>Date: 2019-09-25     <br>Author : dxl
     */
    @ResponseBody
    @ApiOperation(value = "分组能否编辑",notes = "无权限限制；不能重复：组名称-group_name ")
    @RequestMapping(value = "/isEditable",method = {RequestMethod.POST })
    public Result isEditable(@Valid @RequestBody Groups groups) {
        Result result = groupsService.isGroupsEditable(groups);
        return result;
    }

    /**
     * <b> 获得树状目录
     * </b><br><br><i>Description</i> :
     * @return java.util.List<com.dxl.base.ZTreeNode>
     * <br><br>Date: 2019/9/26 16:07     <br>Author : dxl
     */
    @ApiOperation(value = "获得树状目录",notes = "无权限限制；不能重复：组名称-group_name ")
    @RequestMapping(value = "/getTree",method = {RequestMethod.POST })
    @ResponseBody
    public List<Groups> getTree(@RequestParam(value="id", required = false,defaultValue = "0") Integer id){
        List<Groups> groupsList = groupsService.getTree(id);
        return groupsList;
    }

    /**
     * <b> 搜索获得树状目录
     * </b><br><br><i>Description</i> :
     * @return java.util.List<com.dxl.base.ZTreeNode>
     * <br><br>Date: 2019/9/26 16:07     <br>Author : dxl
     */
    @ApiOperation(value = "搜索获得树状目录",notes = "无权限限制；不能重复：组名称-group_name ")
    @RequestMapping(value = "/getTreeBySearch",method = {RequestMethod.GET})
    @ResponseBody
    public List<Groups> getTreeBySearch(@RequestParam(value="groupName", required = true) String groupName,@RequestParam(value="id", required = false,defaultValue = "0") Integer id){
        List<Groups> groupsList = new ArrayList<>();
        if(id.equals(0)){
            Groups groups = new Groups();
            groups.setGroupName(groupName);
            groupsList = (List<Groups>) groupsService.getLikeGroups(groups).getData();
        }else {
            groupsList = groupsService.getTree(id);
        }
        return groupsList;
    }
}
